<template>
  <div class="app-container dialog-w80">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-select filterable placeholder="选择门店" v-model="formInline.store_id" @change="gethouse">
            <el-option
              v-for="item in store_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            filterable
            placeholder="选择调出仓"
            v-model="formInline.house_id"
            @change="houseChange"
          >
            <el-option
              v-for="item in house_list"
              :key="item.house_id"
              :label="item.house_name"
              :value="item.house_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="选择类型" v-model="formInline.species" filterable>
            <el-option v-for="item in type_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.accessories_id"
            placeholder="请输入物品编号/名称"
            @keyup.enter.native="searchClick"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item>
                        <el-input v-model="formInline.accessories_name" placeholder="物品名称"></el-input>
        </el-form-item>-->
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
        <el-form-item class="pull-right">
          <el-button size="small" type="primary" @click="addCanalClick()">新增店内仓</el-button>
        </el-form-item>
      </el-form>
      <transition name="fade" mode="out-in">
        <!-- 列表 -->
        <el-table
          highlight-current-row
          :data="list"
          border
          @sort-change="sortChange"
          size="small"
          :key="formInline.house_id"
        >
          <el-table-column align="center" label="图片" width="80">
            <template slot-scope="scope">
              <vue-preview class="preview" :slides="scope.row.imgList"></vue-preview>
            </template>
          </el-table-column>
          <el-table-column align="center" label="物品名称" prop="name"></el-table-column>
          <el-table-column align="center" label="物品编号" prop="accessories_id"></el-table-column>
          <el-table-column align="center" label="神采编号" sortable>
            <template slot-scope="scope">{{scope.row.no_obsolete}}</template>
          </el-table-column>
          <el-table-column align="center" label="类别" prop="type_name"></el-table-column>
          <el-table-column align="center" label="成本价" prop="money_cost"></el-table-column>
          <el-table-column align="center" label="前台销售">
            <template slot-scope="scope">{{scope.row.is_store_on_sale==1?'支持':'-'}}</template>
          </el-table-column>
          <el-table-column align="center" label="库存量" sortable>
            <template slot-scope="scope">{{scope.row.num}}</template>
          </el-table-column>
          <!-- 列表的操作按钮 -->
          <el-table-column align="center" label="加入调拨">
            <template slot-scope="scope">
              <el-button
                style="font-size:28px"
                :disabled="scope.row.in_car==1"
                type="text"
                @click="addcar(scope.row.id,scope.row.box_num,formInline.store_id,formInline.house_id,scope.$index)"
              >
                <i class="el-icon-circle-plus"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </transition>
      <!-- 物品调拨审单弹框 -->
      <el-dialog
        :close-on-click-modal="false"
        class="dialog_table"
        title="物品调拨审单"
        :visible.sync="show_carList"
        @close="car_close"
      >
        <div class="customHeader">
          <el-select placeholder="选择调入仓" filterable v-model="houseVal">
            <el-option
              v-for="item in houseList"
              :key="item.house_id"
              :label="item.house_name"
              :value="item.house_id"
            ></el-option>
          </el-select>
        </div>
        <el-table
          highlight-current-row
          :data="carList"
          v-loading.body="listLoading"
          border
          size="small"
        >
          <el-table-column align="center" label="图片" width="70">
            <template slot-scope="scope">
              <img :src="scope.row.thumb" width="50" height="50" alt />
            </template>
          </el-table-column>
          <el-table-column align="center" label="名称" prop="name"></el-table-column>
          <el-table-column align="center" label="物品编号" prop="accessories_id"></el-table-column>
          <el-table-column align="center" label="库存量" prop="num"></el-table-column>
          <el-table-column align="center" label="调拨数量(个)">
            <template slot-scope="scope">
              <el-input
                onkeypress="return event.which>=48&&event.which<=57"
                type="number"
                onmousewheel="return false;"
                v-model="scope.row.accessories_num"
                placeholder="数量"
                @input="count"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="成本价">
            <template slot-scope="scope">
              <!-- 保留小数 -->
              {{scope.row.money_cost|keepTwoNum}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="金额总计">
            <template slot-scope="scope">
              <!-- 保留小数 -->
              {{scope.row.money_cost*scope.row.accessories_num|keepTwoNum}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                @click="delClick(scope.row.accessories_id,formInline.store_id,formInline.house_id,scope.$index)"
                type="danger"
                icon="el-icon-delete"
                size="mini"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="bottom_box">
          <el-button
            type="text"
            size="small"
            @click="delCarListClick(formInline.store_id,formInline.house_id)"
          >清空调拨单</el-button>
          <el-button
            :loading="btnLoding"
            class="pull-right"
            size="small"
            type="primary"
            @click="saveClick()"
          >确认调拨</el-button>
          <p class="pull-right" style="line-height:0">
            物品种类:{{car_accessories_num}} 调拨单物品数:{{totalOrder}}
            合计:{{totalPrice|keepTwoNum}}
          </p>
        </div>
      </el-dialog>
      <!-- 新增渠道弹框 -->
      <el-dialog
        :close-on-click-modal="false"
        width="300px"
        title="新增店内仓"
        :visible.sync="addCanalDialog"
        append-to-body
      >
        <el-form :model="addCanalForm" :inline="true" class="demo-form-inline">
          <el-form-item label="店内仓">
            <el-select
              placeholder="店内仓"
              v-model="addCanalForm.is_accessories_machine"
              style="width:246px"
            >
              <el-option
                v-for="item in house_type_list"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-checkbox v-model="isZero" label="此渠道的出售物品价格是否为0" border></el-checkbox>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="saveCanalClick()" size="small">保 存</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination
        class="pull-left"
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
      <el-badge :value="car_accessories_num" class="item pull-right">
        <el-button
          type="text"
          size="small"
          @click="goodscar(formInline.store_id,formInline.house_id,2)"
        >
          <svg-icon style="font-size:18px" icon-class="car" />
        </el-button>&nbsp;
      </el-badge>
    </div>
  </div>
</template>

<script>
import {
  store,
  house,
  getList,
  getPage,
  addcar_submit,
  car,
  delaccessories,
  delCarList,
  save,
  get_house_type,
  saveCana,
  get_goods_type,
} from "@/api/storage_control/pj_in_allocating.js";
export default {
  data() {
    return {
      btnLoding: false,
      type_list: null,
      listLoading: false,
      currentPage: 1,
      totalDataNumber: 0,
      formInline: {
        store_id: "",
        house_id: "",
        accessories_id: "",
        order_by: "",
        species: "",
        // conditions: '3'
      },
      houseVal: "",
      store_list: [],
      house_list: [],
      house_type_list: [],
      list: null,
      show_carList: false,
      addCanalDialog: false,
      carList: [],
      car_accessories_num: "",
      houseList: "",
      isZero: false,
      addCanalForm: {
        species: "",
        store_id: "",
        is_zero: "",
        is_accessories_machine: "",
      },
      saveForm: {},
      totalOrder: "",
      totalPrice: "",
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.type_list) {
        vm.getGoods();
        vm.getHouseType();
        vm.getstore();
      }
    });
  },
  // created() {
  //     this.getstore();
  //     this.getHouseType();
  //     this.getGoods();
  // },
  methods: {
    houseChange() {
      this.fetchData();
    },
    getGoods() {
      get_goods_type({ category_id: this.$route.meta.id }).then((response) => {
        response.forEach((item) => {
          if (item.name == "配件类物品") {
            item.id = 1;
          } else if (item.name == "财务类物品") {
            item.id = 2;
          } else if (item.name == "其他类物品") {
            item.id = 3;
          }
        });
        this.type_list = response;
        this.formInline.species = response.length > 0 ? response[0].id : "";
      });
    },
    sortChange(column, prop, order) {
      this.listLoading = true;
      if (column.order == "ascending") {
        if (column.column.label == "神采编号") {
          this.formInline.order_by = 1;
        } else {
          this.formInline.order_by = 3;
        }
      } else if (column.order == "descending") {
        if (column.column.label == "神采编号") {
          this.formInline.order_by = 2;
        } else {
          this.formInline.order_by = 4;
        }
      } else {
        this.formInline.order_by = "";
      }
      this.fetchData();
    },
    getstore() {
      store({ category_id: 332 }).then((response) => {
        this.store_list = response;
        let data = response.filter(
          (res) => res.id == this.$store.getters.optionId
        );
        if (data.length > 0) {
          this.formInline.store_id = data[0].id;
        } else {
          if (this.store_list.length > 0) {
            this.formInline.store_id = this.store_list[0].id;
          }
        }
        this.gethouse(response[0].id);
      });
    },
    getHouseType() {
      get_house_type({ category_id: 332 }).then((response) => {
        this.house_type_list = response;
      });
    },
    gethouse(id) {
      this.formInline.house_id = "";
      house({ store_id: this.formInline.store_id, cang: 2 }).then(
        (response) => {
          this.house_list = response;
          if (response.length > 0) {
            this.formInline.house_id = response[0].house_id;
          }

          this.fetchData();
        }
      );
    },
    searchClick() {
      if (this.formInline.store_id) {
        this.fetchData();
      } else {
        this.$message({ type: "error", message: "请选择门店" });
      }
    },
    goodscar(store_id, house_id, type) {
      this.listLoading = true;
      car({ store_id: store_id, house_id: house_id, type: type })
        .then((response) => {
          response.forEach(function (val, i) {
            val.money_cost = ((val.money_cost * 1) / 1000000).toFixed(2);
          });
          this.carList = response;
          this.listLoading = false;
          this.show_carList = true;
          this.count();
        })
        .catch((res) => {
          this.listLoading = false;
        });
      house({ store_id: this.formInline.store_id, cang: 2 }).then(
        (response) => {
          this.houseVal = "";
          this.houseList = response;
        }
      );
    },
    // 新增渠道
    addCanalClick() {
      this.addCanalDialog = true;
      this.addCanalForm.is_accessories_machine = "";
    },
    // 新增渠道提交
    saveCanalClick() {
      if (!this.addCanalForm.is_accessories_machine) {
        this.$message({
          type: "error",
          message: "选择店内仓",
        });
        return;
      }
      this.addCanalForm.is_zero = this.isZero ? 1 : 0;
      this.addCanalForm.store_id = this.formInline.store_id;
      saveCana(this.addCanalForm).then((response) => {
        this.$message({
          type: response.code == 1 ? "success" : "error",
          message: response.message,
        });
        if (response.code == 1) {
          this.addCanalDialog = false;
          this.gethouse(this.formInline.store_id);
        }
      });
    },
    //确认调拨
    saveClick() {
      if (this.carList.length == 0) {
        this.$message({ type: "error", message: "大佬，你购物车空空如也" });
        return;
      }
      let err = false;
      this.saveForm = {
        store_id: this.formInline.store_id,
        type: 1,
        house_in: this.houseVal,
        house_out: this.formInline.house_id,
        accessories: [],
        species: this.formInline.species,
      };
      if (!this.houseVal) {
        this.$message({ type: "error", message: "请选择调入仓" });
        return;
      }
      if (this.houseVal == this.formInline.house_id) {
        this.$message({ type: "error", message: "请选择正确的调入仓" });
        return;
      }
      for (var i = 0; i < this.carList.length; i++) {
        if (this.carList[i].accessories_num * 1 > this.carList[i].num * 1) {
          err = true;
          break;
        }
      }
      if (err) {
        this.$message({ type: "error", message: "调拨数不可大于当前库存数" });
        return;
      }
      for (var i = 0; i < this.carList.length; i++) {
        this.saveForm.accessories.push({
          accessories_id: this.carList[i].accessories_id,
          num: this.carList[i].accessories_num,
          look_money: this.carList[i].money_cost * 1000000,
          money_cost: this.carList[i].money_cost * 1000000,
        });
      }
      var data = this.saveForm;
      this.btnLoding = true;
      save(data)
        .then((response) => {
          this.$message({
            type: response.code == 1 ? "success" : "error",
            message: response.message,
          });
          if (response.code == 1) {
            this.btnLoding = false;
            this.show_carList = false;
          }
        })
        .catch(() => {
          this.btnLoding = false;
        });
    },
    // 计算购物车
    count() {
      var totalPrice = 0; //临时总价
      var totalOrder = 0;
      this.carList.forEach(function (val, index) {
        // if (val.is_on_sale * 1 > 0) {
        totalPrice += val.accessories_num * 1 * val.money_cost * 1; //累计总价
        totalOrder += val.accessories_num * 1;
        // }
      });
      console.log(totalOrder);
      this.totalPrice = parseFloat(totalPrice);
      this.totalOrder = parseFloat(totalOrder);
    },
    addcar(accessories_id, num, store_id, house_id, index) {
      var data = {
        accessories_id: accessories_id,
        type: 2,
        num: num,
        store_id: store_id,
        house_id: house_id,
      };
      this.listLoading = true;
      addcar_submit(data)
        .then((response) => {
          this.listLoading = false;
          if (response.code == 1) {
            this.list[index].in_car = 1;
            this.car_accessories_num += 1;
            this.$message({
              type: "success",
              message: "添加成功",
            });
          }
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    // 清空购物车
    delCarListClick(store_id, house_id) {
      delCarList({ store_id: store_id, house_id: house_id, type: 2 }).then(
        (response) => {
          this.carList = [];
        }
      );
    },
    delClick(accessories_id, store_id, house_id, index) {
      delaccessories({
        accessories_id: accessories_id,
        house_id: house_id,
        store_id: store_id,
        type: 2,
      }).then((response) => {
        this.$message({
          type: response.code == 1 ? "success" : "error",
          message: response.message,
        });
        if (response.code == 1) {
          this.car_accessories_num--;
          this.carList.splice(index, 1);
          this.count();
        }
      });
    },
    car_close() {
      this.handleCurrentChange(this.currentPage);
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then((response) => {
          this.totalDataNumber = response * 20;
          getList(this.formInline).then((response) => {
            this.car_accessories_num =
              response.length > 0 ? response[0].car_accessories_num : 0;
            response.forEach((val, i) => {
              val.imgList = [
                {
                  src: val.img,
                  msrc: val.thumb,
                  w: 1200,
                  h: 900,
                },
              ];
            });
            this.list = response;
            this.listLoading = false;
          });
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then((response) => {
        this.car_accessories_num =
          response.length > 0 ? response[0].car_accessories_num : 0;
        response.forEach((val, i) => {
          val.imgList = [
            {
              src: val.img,
              msrc: val.thumb,
              w: 1200,
              h: 900,
            },
          ];
        });
        this.list = response;
        this.listLoading = false;
      });
    },
  },
};
</script>